//
//  UIBarButtonItem+EXtension.swift
//  weiboSina
//
//  Created by admin on 15/12/28.
//  Copyright © 2015年 admin. All rights reserved.
//

import UIKit
extension UIBarButtonItem{
//用便利构造函数

//swift函数的参数可以给一个默认值，外界可以不传这个参数，使用默认值
convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector){
    self.init()
  //初始化一个按钮
    let button = UIButton()
    button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
    
    if imageName != nil{
    
    //设置按钮的不同状态
        button.setImage(UIImage(named: imageName!), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName!)_highlighted"), forState: .Highlighted)
    
    
    }

    //如果外界传入标题，就设置标题
    if title != nil {
    
    button.setTitle(title, forState: .Normal)
        //设置字体的大小颜色
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor(white: 80/255.0, alpha: 1.0), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
    }
    //设置按钮的大小
    button.sizeToFit()
    //将按钮设置成当前类的customView
    customView = button
    

}
}
